import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login';
import Home from '@/components/Home'
import Welcome from '@/components/Welcome.vue';
import Users from '@/components/Users/Users.vue';
Vue.use(Router)


const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    redirect: "/Welcome",
    children: [
      { path: "/welcome", component: Welcome },
      { path: "/Users", component: Users }
    ]
  }
]
const router = new Router({
  routes
})
router.beforeEach((to, from, next) => {
  // to 将访问哪一个路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数,表示放行
  //   next() 放行 next('/login') 强制跳转
  if (to.path == '/login')
    return next()
  const token = window.sessionStorage.getItem('token');
  if (!token)
    return next('/login')
  return next()
})
export default router